home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / newmat08.zip / TMTG.CPP < prev    next >
C/C++ Source or Header  |  1995-01-11  |  4KB  |  137 lines

  1.  
  2. //#define WANT_STREAM
  3.  
  4. #include "include.h"
  5.  
  6. #include "newmatap.h"
  7.  
  8. void Print(const Matrix& X);
  9. void Print(const UpperTriangularMatrix& X);
  10. void Print(const DiagonalMatrix& X);
  11. void Print(const SymmetricMatrix& X);
  12. void Print(const LowerTriangularMatrix& X);
  13.  
  14. void Clean(Matrix&, Real);
  15.  
  16.  
  17.  
  18.  
  19. void trymatg()
  20. {
  21. //   cout << "\nSixteenth test of Matrix package\n";
  22. //   cout << "\n";
  23.    Tracer et("Sixteenth test of Matrix package");
  24.    Exception::PrintTrace(TRUE);
  25.  
  26.    int i,j;
  27.    Matrix M(4,7);
  28.    for (i=1; i<=4; i++) for (j=1; j<=7; j++) M(i,j) = 100 * i + j;
  29.    ColumnVector CV = M.AsColumn();
  30.    {
  31.       Tracer et1("Stage 1");
  32.       RowVector test(7);
  33.       test(1) = SumSquare(M);
  34.       test(2) = SumSquare(CV);
  35.       test(3) = SumSquare(CV.t());
  36.       test(4) = SumSquare(CV.AsDiagonal());
  37.       test(5) = SumSquare(M.AsColumn());
  38.       test(6) = Matrix(CV.t()*CV)(1,1);
  39.       test(7) = (CV.t()*CV).AsScalar();
  40.       test = test - 2156560.0; Print(test);
  41.    }
  42.  
  43.    UpperTriangularMatrix U(6);
  44.    for (i=1; i<=6; i++) for (j=i; j<=6; j++) U(i,j) = i + (i-j) * (i-j);
  45.    M = U; DiagonalMatrix D; D << U;
  46.    LowerTriangularMatrix L = U.t(); SymmetricMatrix S; S << (L+U)/2.0;
  47.    {
  48.       Tracer et1("Stage 2");
  49.       RowVector test(6);
  50.       test(1) = U.Trace();
  51.       test(2) = L.Trace();
  52.       test(3) = D.Trace();
  53.       test(4) = S.Trace();
  54.       test(5) = M.Trace();
  55.       test(6) = ((L+U)/2.0).Trace();
  56.       test = test - 21; Print(test);
  57.       test.ReDimension(5);
  58.       test(1) = LogDeterminant(U).Value();
  59.       test(2) = LogDeterminant(L).Value();
  60.       test(3) = LogDeterminant(D).Value();
  61.       test(4) = LogDeterminant(D).Value();
  62.       test(5) =  LogDeterminant((L+D)/2.0).Value(); // (!=Glockenspiel)
  63.       test = test - 720; Clean(test,0.000000001); Print(test);
  64.    }
  65.  
  66.    {
  67.       Tracer et1("Stage 3");
  68.       S << L*U; M = S;
  69.       RowVector test(4);
  70.       test(1) = LogDeterminant(S).Value();
  71.       test(2) = LogDeterminant(M).Value();
  72.       test(3) = LogDeterminant(L*U).Value();           // (!=Glockenspiel)
  73.       test(4) = LogDeterminant(Matrix(L*L)).Value();   // (!=Glockenspiel)
  74.       test = test - 720.0 * 720.0; Clean(test,0.000000001); Print(test);
  75.    }
  76.  
  77.    {
  78.       Tracer et1("Stage 4");
  79.       M = S * S;
  80.       Matrix SX = S;
  81.       RowVector test(3);
  82.       test(1) = SumSquare(S);
  83.       test(2) = SumSquare(SX);
  84.       test(3) = Trace(M);
  85.         test = test - 3925961.0; Print(test);
  86.    }
  87.    {
  88.       Tracer et1("Stage 5");
  89.       SymmetricMatrix SM(10), SN(10);
  90.       Real S = 0.0;
  91.       for (i=1; i<=10; i++) for (j=i; j<=10; j++)
  92.       {
  93.          SM(i,j) = 1.5 * i - j; SN(i,j) = SM(i,j) * SM(i,j);
  94.          if (SM(i,j) < 0.0)   SN(i,j) = - SN(i,j);
  95.          S += SN(i,j) * ((i==j) ? 1.0 : 2.0);
  96.       }
  97.       Matrix M = SM, N = SN; RowVector test(4);
  98.       test(1) = SumAbsoluteValue(SN);
  99.       test(2) = SumAbsoluteValue(-SN);
  100.       test(3) = SumAbsoluteValue(N);
  101.       test(4) = SumAbsoluteValue(-N);
  102.       test = test - 1168.75; Print(test);
  103.       test(1) = Sum(SN);
  104.       test(2) = -Sum(-SN);
  105.       test(3) = Sum(N);
  106.       test(4) = -Sum(-N);
  107.       test = test - S; Print(test);
  108.       test(1) = MaximumAbsoluteValue(SM);
  109.       test(2) = MaximumAbsoluteValue(-SM);
  110.       test(3) = MaximumAbsoluteValue(M);
  111.       test(4) = MaximumAbsoluteValue(-M);
  112.       test = test - 8.5; Print(test);
  113.    }
  114.    {
  115.       Tracer et1("Stage 6");
  116.       Matrix M(15,20); Real value = 0.0;
  117.       for (i=1; i<=15; i++) { for (j=1; j<=20; j++) M(i,j) = 1.5 * i - j; }
  118.       for (i=1; i<=20; i++)
  119.       { Real v = SumAbsoluteValue(M.Column(i)); if (value<v) value = v; }
  120.       RowVector test(3);
  121.       test(1) = value;
  122.       test(2) = Norm1(M);
  123.       test(3) = NormInfinity(M.t());
  124.       test = test - 165; Print(test);
  125.       test.ReDimension(5);
  126.       ColumnVector CV = M.AsColumn(); M = CV.t();
  127.       test(1) = Norm1(CV.t());
  128.       test(2) = MaximumAbsoluteValue(M);
  129.       test(3) = NormInfinity(CV);
  130.       test(4) = Norm1(M);
  131.       test(5) = NormInfinity(M.t());
  132.       test = test - 21.5; Print(test);
  133.    }   
  134.  
  135. //   cout << "\nEnd of Sixteenth test\n";
  136. }
  137.